package cn.wolfcode.shiro;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.mapper.RoleMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.credential.CredentialsMatcher;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CRMRealm extends AuthorizingRealm {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    public void setCredentialsMatcher(CredentialsMatcher credentialsMatcher) {
        super.setCredentialsMatcher(credentialsMatcher);
    }

    /**
     * 提供认证信息的数据
     *
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //通过token获取到用户名
        UsernamePasswordToken token = (UsernamePasswordToken)authenticationToken;
        String username = token.getUsername();
        //String username = (String) token.getPrincipal();
        Employee employee = employeeMapper.selectByName(username);
        if (employee != null) {
            if(employee.isStatus()){
//                throw new AuthenticationException("该账号已被禁用");
            }else {
                //第一个参数是新员工对象，后续可以通过subject获取，第二个参数是用户真正的密码，
                // 第三个参数是当前数据源的名字，盐，和注册时使用的盐相同
                return new SimpleAuthenticationInfo(employee, employee.getPassword(),
                        ByteSource.Util.bytes(employee.getName()), this.getName());
            }
        }
        return null;
    }


    /**
     * 提供授权信息数据
     *
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        //获取当前登录的对象
        /*Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();*/
        Employee employee = (Employee) principals.getPrimaryPrincipal();

        //info中数据就是当前登录用户的 权限和角色信息
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();

        //判断是否是超级管理员
        if (!employee.isAdmin()) {
            //根据员工id查询员工的角色编码集合
            List<String> roles = roleMapper.selectSnByEmployeeId(employee.getId());
            info.addRoles(roles);

            //根据员工id查询员工的权限表达式集合
            List<String> expressions = permissionMapper.selectExpressionByEmpId(employee.getId());
            info.addStringPermissions(expressions);
        } else {
            info.addStringPermission("*:*");
            info.addRole("ADMIN");
        }
        return info;
    }
}
